package com.john.blockchain.demo.mock.task;

import com.john.common.util.IncrementString;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author jiangguangtao 2018/4/14
 */
public class HashNonceGeneratorTask implements Runnable {
    private static final Logger LOGGER = LoggerFactory.getLogger(HashNonceGeneratorTask.class);

    private HashCalcTaskContext taskContext;
    private IncrementString incrementString;

    public HashNonceGeneratorTask(HashCalcTaskContext taskContext, String initStr, int maxStringLength) {
        this.taskContext = taskContext;

        incrementString = new IncrementString(initStr, maxStringLength);
    }

    @Override
    public void run() {
        BlockingQueue<String> nonceQueue = taskContext.getNonceQueue();
        while (taskContext.isTaskRunning()) {
            try {
                nonceQueue.offer(incrementString.nextString(), 1, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                LOGGER.warn("添加生成随机数异常{}", e.getMessage());
            }
        }
        LOGGER.info("随机数生成线程结束！");
    }
}
